package org.snowin;



import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.log4j.Logger;
import org.dom4j.Element;
import org.snowin.entities.InterfaceParam;
import org.snowin.entities.InterfaceTestEntity;
import org.snowin.utils.GlobalInfo;
import org.snowin.utils.UrlGenerator;

public class InterfaceTestEngine
{
    String testApi = "";
    List<InterfaceTestEntity> interfaceTests = null;
    String checkString = "";
    String sendType = "post";
    Logger logger;
    List<List<String>> resultList = new ArrayList();
    List<Element> globalVaribles = null;
    UrlGenerator urlGenerator;

    public InterfaceTestEngine(String testApi, List<InterfaceTestEntity> interfaceTests, String sendType, Logger logger, List<Element> globalVaribles)
    {
        this.globalVaribles = globalVaribles;
        this.testApi = testApi;
        this.sendType = sendType;
        this.interfaceTests = interfaceTests;
        this.logger = logger;
        runCase();
    }

    private void runCase()
    {
        int i = 1;
        for (InterfaceTestEntity interfaceTest : this.interfaceTests)
        {
            List<String> singleResult = new ArrayList();
            List<InterfaceParam> paraList = interfaceTest.getInterfaceParams();
            this.urlGenerator = new UrlGenerator(this.testApi, interfaceTest);
            try
            {
                String response = this.urlGenerator.doTest(false, this.sendType);
                String result = "pass";
                Pattern pat = Pattern.compile(interfaceTest.getExpectContansString());

                Matcher mat = pat.matcher(response);
                boolean rs = mat.find();
                if (rs) {
                    result = "pass";
                } else {
                    result = "fail";
                }
                if (response.contains(interfaceTest.getExpectContansString())) {
                    result = "pass";
                }
                if ((response.contains("Notice: Undefined index")) && (response.contains("on line"))) {
                    result = "fail";
                }
                String replaceResponse = "";
                if (response.length() > 50) {
                    replaceResponse = response.substring(0, 50) + "...";
                } else {
                    replaceResponse = response;
                }
                singleResult.add(this.urlGenerator.getUrl());
                singleResult.add(response);
                singleResult.add(result);
                singleResult.add(interfaceTest.getExpectContansString());
                this.logger.info("【Test " + i + "】          " + this.urlGenerator.getUrl());
                this.logger.info("【Response " + i + "】     " + replaceResponse);
                this.logger.info("【expect words】     " + interfaceTest.getExpectContansString());
                this.logger.info("【Result " + i + "】       " + result);
                this.logger.info("............");
                i++;
            }
            catch (Exception e)
            {
                this.logger.error(e.getMessage());
            }
            setVaribles();
            this.resultList.add(singleResult);
        }
    }

    public List<List<String>> getTestResult()
    {
        return this.resultList;
    }

    public String getSendType()
    {
        return this.sendType;
    }

    public void setSendType(String sendType)
    {
        this.sendType = sendType;
    }

    public String getTestApi()
    {
        return this.testApi;
    }

    public void setTestApi(String testApi)
    {
        this.testApi = testApi;
    }

    public String getCheckString()
    {
        return this.checkString;
    }

    public void setCheckString(String checkString)
    {
        this.checkString = checkString;
    }

    public List<InterfaceTestEntity> getParaList()
    {
        return this.interfaceTests;
    }

    public void setParaList(List<InterfaceTestEntity> interfaceTests)
    {
        this.interfaceTests = interfaceTests;
    }

    private void setVaribles()
    {
        for (Element e : this.globalVaribles)
        {
            String varibleName = e.attributeValue("name");
            String varibleVaule = this.urlGenerator.getVarible(e
                    .attributeValue("value"));
            if (!GlobalInfo.varibles.containsKey(varibleName)) {
                GlobalInfo.varibles.put(varibleName, varibleVaule);
            } else {
                GlobalInfo.varibles.replace(varibleName, varibleVaule);
            }
        }
    }
}
